#!/bin/sh
 
DEBUG=1
 
logmsg (){
  if [ $DEBUG -ge 1 ]; then  
    D=`date +"%F %T"`
    echo "[$D] $@"
    echo "[$D] $@" >> /usr/local/etc/debug.log
  fi
}
 
clean_shut (){
        old_i=""
        logmsg "Killing processes for ext3 partitions"
        for i in `cat /proc/mounts | sort -r | awk '/ext3/{print($1)}'` ; do
          if [ ! "$old_i" == "$i" ]; then
            old_i="$i"
            pids=""
            if [ -n "`lsof -t "$i"`" ]; then
              for j in `lsof -t "$i" | sort -nr` ; do
                pids="$pids $j"
              done
              logmsg "Kills for $i"
              logmsg "Kill PIDs: $pids"
              kill -9 "$pids"
              sleep 2
            fi
          fi
        done
        logmsg "Sync"
        sync
        old_i=""
        logmsg "Killing processes for ntfs partitions"
        for i in `cat /proc/mounts | sort -r | awk '/ufsd/{print($1)}'` ; do
          if [ ! "$old_i" == "$i" ]; then
            old_i="$i"
            pids=""
            if [ -n "`lsof -t "$i"`" ]; then
              for j in `lsof -t "$i" | sort -nr` ; do
                pids="$pids $j"
              done
              logmsg "Kills for $i"
              logmsg "Kill PIDs: $pids"
              kill -9 "$pids"
              sleep 2
            fi
          fi
        done
        logmsg "Sync"
        sync
        logmsg "Unmount fake HDD1"
        # Unmount HDD1 fake
        [ -n "`mount | grep ext3 | grep HDD1`" ] && umount /tmp/hdd/volumes/HDD1
        logmsg "Unmount fake webentry"
        # Unmount webentry fake
        [ -n "`mount | grep ext3 | grep webentry`" ] && umount /usr/local/bin/webentry
        logmsg "Unmount real HDD1"
        # Unmount HDD1 real
        [ -n "`mount | grep HDD1`" ] && umount /tmp/hdd/volumes/HDD1
        logmsg "Remount all ext3 to RO"
        for i in `cat /proc/mounts | sort -r | awk '/ext3/{print($1)}'` ; do
          logmsg "Remount $i"
          mount -t ext3 -o remount,ro "$i"
        done
}
 
 
case "$1" in
  start)
        # Power ON, for ....
        # STANDBY_CMD_SYSTEM_ON
        echo -en '\xf0\xfe' >/dev/tts/1
        # STANDBY_CMD_MUTE_OFF
        echo -en '\xf9\xfe' >/dev/tts/1
        ;;
  stop)
        # Power Off, for ....
        logmsg "Sync"
        sync
        #Stop all plays and goto home
        logmsg "Fake Stop"
        echo -en "ec139f00" > /sys/devices/platform/VenusIR/fakekey
        sleep 2
        logmsg "Fake Return"
        echo -en "b04f9f00" > /sys/devices/platform/VenusIR/fakekey
        sleep 2
        logmsg "Fake Return"
        echo -en "b04f9f00" > /sys/devices/platform/VenusIR/fakekey
        sleep 2
        logmsg "Fake Return"
        echo -en "b04f9f00" > /sys/devices/platform/VenusIR/fakekey
        logmsg "Wait for..."
        sleep 2
        # Clean kill and re(un)mount
        logmsg "Call clean_shut"
        clean_shut
        # STANDBY_CMD_SYSTEM_OFF
        logmsg "Poweroff"
        echo -en "a8579f00" > /sys/devices/platform/VenusIR/fakekey
        #echo -en '\xf5\xfe' >/dev/tts/1
        ;;
  *)
        echo $"Usage: $0 {start|stop}"
        ;;
esac
 
exit $?